﻿using HrEFCore;
using HrEFCore.Models;
using HrIService;
using HrService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Data.SqlTypes;
using System.Net;
using System.Runtime.Intrinsics.X86;
using System.Text;

namespace HrUI.Controllers
{
	public class HFController : Controller
	{
		private readonly MyDbContext myDbContext;
		private readonly HFService hFService;
		private readonly CFTKService cFTKService;
		private readonly CMKService cMKService;
		private readonly CMService cMService;
		private readonly CPCService cPCService;
		private readonly IWebHostEnvironment environment;
		private readonly ERService eRService;

		public HFController(MyDbContext myDbContext,HFService hFService,CFTKService cFTKService,
			CMKService cMKService,CMService cMService,CPCService cPCService, IWebHostEnvironment environment,ERService eRService)
        {
			this.myDbContext = myDbContext;
			this.hFService = hFService;
			this.cFTKService = cFTKService;
			this.cMKService = cMKService;
			this.cMService = cMService;
			this.cPCService = cPCService;
			this.environment = environment;
			this.eRService = eRService;
		}
        public async Task<IActionResult> Index()
		{
			return View();
		}
		public async Task<IActionResult> Index2(FenYe2<HF> fy)
		{
			fy=await hFService.SelectAllFYHFAsync(fy);
			return Json(fy);
		}
		public async Task<IActionResult> Add(short resId)
		{
			string uname= HttpContext.Session.GetString("UTrueName");
			//HttpContext.Session.SetInt32("resId2", resId);
			ViewBag.uname = uname;
			ER er = await eRService.ERById(resId);
			ViewData.Model = er;
			await xllb();
			return View();
		}
		public async Task<IActionResult> EMRById()
		{
			short resId = (short)HttpContext.Session.GetInt32("resId2");
			ER er = await eRService.ERById(resId);
			return Json(er);
		}
		public async Task xllb()//下拉列表
		{
			List<CFFK> list = await cFTKService.CFFKSelectAsync();
			SelectList selectListItems = new SelectList(list, "FirstKindId", "FirstKindName");
			ViewBag.cffk = selectListItems;//一级

			List<CFSK> list2 = await cFTKService.CFSKSelectAsync();
			SelectList selectListItems1 = new SelectList(list2, "SecondKindId", "SecondKindName");
			ViewBag.cfsk = selectListItems1;//二级

			List<CFTK> list3 = await cFTKService.SelectCFTKAsync();
			SelectList selectListItems2 = new SelectList(list3, "ThirdKindId", "ThirdKindName");
			ViewBag.cftk = selectListItems2;//三级

			List<CMK> list4 = await cMKService.InquireAll();
			SelectList selectListItems3 = new SelectList(list4, "MajorKindId", "MajorKindName");
			ViewBag.cmk = selectListItems3;//职位分类

			List<CM> list5 = await cMService.CMSelectAsync();
			SelectList selectListItems4 = new SelectList(list5, "MajorId", "MajorName");
			ViewBag.cm= selectListItems4;//职位名称

			List<CPC> list6 = await cPCService.InquireJobTitle();
			SelectList selectListItems5 = new SelectList(list6, "AttributeName", "AttributeName");
			ViewBag.zc = selectListItems5;//职称

			List<CPC> list7 = await cPCService.Inquirenationnal();
			SelectList selectListItems6 = new SelectList(list7, "AttributeName", "AttributeName");
			ViewBag.gj = selectListItems6;//国籍

			List<CPC> list8 = await cPCService.InquireNation();
			SelectList selectListItems7 = new SelectList(list8, "AttributeName", "AttributeName");
			ViewBag.mz = selectListItems7;//民族

			List<CPC> list9 = await cPCService.InquireZjxy();
			SelectList selectListItems8 = new SelectList(list9, "AttributeName", "AttributeName");
			ViewBag.zjxy = selectListItems8;//宗教信仰

			List<CPC> list10 = await cPCService.InquireZzmm();
			SelectList selectListItems9 = new SelectList(list10, "AttributeName", "AttributeName");
			ViewBag.zzmm = selectListItems9;//政治面貌

			List<CPC> list11 = await cPCService.Inquirexl();
			SelectList selectListItems10 = new SelectList(list11, "AttributeName", "AttributeName");
			ViewBag.xl = selectListItems10;//学历

			List<CPC> list12 = await cPCService.InquireJynx();
			SelectList selectListItems11 = new SelectList(list12, "AttributeName", "AttributeName");
			ViewBag.jynx = selectListItems11;//教育年限

			List<CPC> list13 = await cPCService.InquireZy();
			SelectList selectListItems12 = new SelectList(list13, "AttributeName", "AttributeName");
			ViewBag.xlzy = selectListItems12;//学历专业

			List<CPC> list14 = await cPCService.InquireTc();
			SelectList selectListItems13 = new SelectList(list14, "AttributeName", "AttributeName");
			ViewBag.tc = selectListItems13;//特长

			List<CPC> list15 = await cPCService.InquireAh();
			SelectList selectListItems14 = new SelectList(list15, "AttributeName", "AttributeName");
			ViewBag.ah = selectListItems14;//爱好

			//List<SSD> list16 = await hFService.SSDXlAsync();
			//SelectList selectListItems15 = new SelectList(list16, "StandardId", "StandardName");
			//ViewBag.xc = selectListItems15;//薪酬
			List<SS> list16 = await hFService.SSXLAsync();
			SelectList selectListItems15 = new SelectList(list16, "StandardId", "StandardName");
			ViewBag.xc = selectListItems15;//薪酬
		}
		public async Task<List<JiLian>> GetFirstKinds()
		{
			List<CFFK> cf = await cFTKService.CFFKSelectAsync();
			List<JiLian> jl=new List<JiLian>();
			foreach (CFFK item in cf)
			{
				JiLian jilian = new JiLian()
				{
					value = item.FirstKindId,
					label = item.FirstKindName,
					children = await GetSecondKinds(item.FirstKindId)
				};
				jl.Add(jilian);
			}
			return jl;
		}
		public async Task<List<JiLian>> GetSecondKinds(string id)
		{
			List<CFSK> cs = await cFTKService.CFSKLDAsync(id);
			List<JiLian> jl= new List<JiLian>();
			foreach(CFSK item in cs)
			{
				JiLian jiLian = new JiLian()
				{
					value = item.SecondKindId,
					label = item.SecondKindName,
					children = await GetThirdKinds(item.SecondKindId)
				};
				jl.Add(jiLian);
			}
			return jl;
		}
		public async Task<List<JiLian>> GetThirdKinds(string id)//三级机构
		{
			List<CFTK> cf= await cFTKService.CFTKLDAsync(id);
			List<JiLian> jl = new List<JiLian>();
			foreach (CFTK item in cf)
			{
				JiLian jiLian = new JiLian()
				{
					value = item.ThirdKindId,
					label = item.ThirdKindName,
				};
				jl.Add(jiLian);
			}
			return jl;
		}
		[HttpPost]
		public IActionResult WJSC(IFormFileCollection file)//文件上传
		{
			foreach (var item in file)
			{
				//生成文件名
				string name = DateTime.Now.ToString("yyyyMMddHHmmssfff");
				//获取后缀名
				string ext = item.FileName.Substring(item.FileName.LastIndexOf('.'));
				//设置上传路径
				string path = $"Uploader/{DateTime.Now.ToString("yyyy/MM/dd/")}" + name + ext;
				//生成文件的绝对路径
				var uploads = Path.Combine(environment.WebRootPath, path);
				//ViewBag.url = uploads;
				HttpContext.Session.SetString("url",path);
				//创建上传文件对应的文件夹
				(new FileInfo(uploads)).Directory.Create();
				using (var stream = new FileStream(uploads, FileMode.CreateNew))
				{
					item.CopyTo(stream);//图片上传
										//TempData["name"] = name + ext;//保存图片的值
				}
				//return uploads;
			}
			return Content("ok");

		}
		public async Task<IActionResult> GetName(string id)//根据职位分类拿到职位名称
		{
			List<CM> cm = await hFService.SelectNameLD(id);
			return Json(cm);
		}
		[HttpPost]
		public async Task<IActionResult> Add(HF hf)//添加
		{
			//hf.HumanPicture= HttpContext.Session.GetString("url");
			//CMK cmk = await hFService.SelectCMKById(hf.HumanMajorKindId);
			//hf.HumanMajorKindName = cmk.MajorKindName;//职位分类名称

			//CM cm = await hFService.SelectCMById(hf.HumanMajorId);
			//hf.HunmaMajorName = cm.MajorName;//职位名称

			//SSD ssd = await hFService.SelectSSDById(hf.SalaryStandardId);
			//hf.SalaryStandardName = ssd.StandardName;//SSD薪酬标准	

			SS ss = await hFService.SelectSSById(hf.SalaryStandardId);
			hf.SalaryStandardName = ss.StandardName;//SS表薪酬标准

			CFFK cffk = await cFTKService.CFFKByIdAsync(hf.FirstKindId);
			hf.FirstKindName = cffk.FirstKindName;//一级

			CFSK cfsk = await cFTKService.CFSKByIdAsync(hf.SecondKindId);
			hf.SecondKindName = cfsk.SecondKindName;//二级

			CFTK cftk = await hFService.cftkById(hf.ThirdKindId);
            hf.ThirdKindName = cftk.ThirdKindName;//三级
			hf.SalarySum = ss.SalarySum;
            int row = await hFService.AddHFAsync(hf);

			if(row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("Add");
			}
		}
		public async Task<IActionResult> FuHe(short id)//复核，根据id查看
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;
			HF hf = await hFService.SelectHFById(id);
			ViewBag.img = hf.HumanPicture;
			ViewData.Model = hf;
			await xllb();
			return View();
		}
		public async Task<IActionResult> FuHeUpdate(HF hf)//复核通过
		{
			hf.CheckStatus = 1;
			//hf.CheckTime = DateTime.Now;
			int row= await hFService.FuHeTGHF(hf);
			if(row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("FuHe", new {id=hf.HufId});
			}
			
		}
		public async Task<IActionResult> Select()//条件查询的页面
		{
			await xllb();
			return View();
		}
		public IActionResult SouSuo()//点击搜索按钮，搜索页面出来
		{
			return View(); 
		}
		public async Task<IActionResult> SelectByName(HF hf)//搜索里面的开始按钮
		{
			if (hf.HumanName != null)
			{
				HttpContext.Session.SetString("HumanName", hf.HumanName);
			}
			else if(hf.HumanName == null)
			{
				HttpContext.Session.SetString("HumanName", "");
			}
			
			List<HF> list = await hFService.SelectSouSuoHF(hf.HumanName);
			if (list != null)
			{
				return View();
			}
			else
			{
				return RedirectToAction("SouSuo");
			}
		}
		public async Task<IActionResult> SelectByTime(HF hf,DateTime startDate,DateTime endDate)//档案查询的查询按钮
		{
			//DateTime startdate = startDate;
			//DateTime enddate = endDate;
			if(startDate != DateTime.MinValue)
			{
				HttpContext.Session.SetString("startDate", startDate.ToString());
			}
			else
			{
				HttpContext.Session.SetString("startDate", "");
			}
			if(endDate != DateTime.MinValue)
			{
				HttpContext.Session.SetString("endDate", endDate.ToString());
			}
			else
			{
				HttpContext.Session.SetString("endDate", "");
			}
			if (hf.HumanMajorKindId == null)
			{
				HttpContext.Session.SetString("HumanMajorKindName", "");
			}
			else
			{
				CMK cmk = await hFService.SelectCMKById(hf.HumanMajorKindId);
				hf.HumanMajorKindName = cmk.MajorKindName;//职位分类名称
				HttpContext.Session.SetString("HumanMajorKindName", hf.HumanMajorKindName);
			}
			
			if(hf.HumanMajorId == null)
			{
				HttpContext.Session.SetString("HunmaMajorName", "");
			}
			else
			{
				CM cm = await hFService.SelectCMById(hf.HumanMajorId);
				hf.HunmaMajorName = cm.MajorName;//职位名称	
				HttpContext.Session.SetString("HunmaMajorName", hf.HunmaMajorName);
			}

			if (hf.FirstKindId==null)
			{
				HttpContext.Session.SetString("FirstKindName", "");
			}
			else
			{
				CFFK cffk = await cFTKService.CFFKByIdAsync(hf.FirstKindId);
				hf.FirstKindName = cffk.FirstKindName;//一级
				HttpContext.Session.SetString("FirstKindName", hf.FirstKindName);
			}

			if (hf.SecondKindId == null)
			{
				HttpContext.Session.SetString("SecondKindName", "");
			}
			else
			{
				CFSK cfsk = await cFTKService.CFSKByIdAsync(hf.SecondKindId);
				hf.SecondKindName = cfsk.SecondKindName;//二级
				HttpContext.Session.SetString("SecondKindName", hf.SecondKindName);
			}

			if(hf.ThirdKindId != null)
			{
				CFTK cftk = await hFService.cftkById(hf.ThirdKindId);
				hf.ThirdKindName = cftk.ThirdKindName;//三级
				HttpContext.Session.SetString("ThirdKindName", hf.ThirdKindName);
			}
			else
			{
				HttpContext.Session.SetString("ThirdKindName", "");
			}
			return View();
		}
		public IActionResult SelectByNameYM(FenYe2<HF> list)
		{
			return View(list);
		}
		public async Task<IActionResult> SelectFYByName(int currentPage,int pageSize)//模糊查询
		{
			string start= HttpContext.Session.GetString("startDate");
			string end = HttpContext.Session.GetString("endDate");
			string huname = HttpContext.Session.GetString("HumanName");
			string firstName = HttpContext.Session.GetString("FirstKindName");
			string secondtName = HttpContext.Session.GetString("SecondKindName");
			string thirdName = HttpContext.Session.GetString("ThirdKindName");
			string MajorKindName = HttpContext.Session.GetString("HumanMajorKindName");
			string MajorName = HttpContext.Session.GetString("HunmaMajorName");
			StringBuilder where = new StringBuilder();
			if (start != "" && end != "")
			{
				where.Append($" and RegistTime between '{start}' and '{end}'");
			}
			else if(start != "" && end == "")
			{
				where.Append($" and RegistTime > '{start}'");
			}
			else if(start == "" && end != "")
			{
				where.Append($" and RegistTime < '{end}'");
			}
            else
            {
				where.Append($" ");
            }

            if (huname == null)
			{
				 where.Append($" and HumanName like '%%'");
			}else
			{
				 where.Append($" and HumanName like '%{huname}%'");
			}
			if (firstName == null)
			{
				where.Append($" and FirstKindName like '%%'");
			}
			else
			{
				where.Append($" and FirstKindName like '%{firstName}%'");
			}
			if (secondtName == null)
			{
				where.Append($" and SecondKindName like '%%'");
			}
			else
			{
				where.Append($" and SecondKindName like '%{secondtName}%'");
			}
			if (thirdName == null)
			{
				where.Append($" and ThirdKindName like '%%'");
			}
			else
			{
				where.Append($" and ThirdKindName like '%{thirdName}%'");
			}
			if (MajorKindName == null)
			{
				where.Append($" and HumanMajorKindName like '%%'");
			}
			else
			{
				where.Append($" and HumanMajorKindName like '%{MajorKindName}%'");
			}
			if (MajorName == null)
			{
				where.Append($" and HunmaMajorName like '%%'");
			}
			else
			{
				where.Append($" and HunmaMajorName like '%{MajorName}%'");
			}
			FenYe2<HF> fy= await hFService.SelectFYTJHF(currentPage,pageSize,where.ToString());
			return Json(fy);
		}

		public IActionResult CXUpd()//有修改的查询页面
		{
			return View();
		}
		public async Task<IActionResult> Upd(short id)//点击修改，根据id查看
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;
			HF hf = await hFService.SelectHFById(id);
			ViewBag.img = hf.HumanPicture;
			ViewData.Model = hf;
			await xllb();
			return View();
		}
		public async Task<IActionResult> Update(HF hf)//点击修改按钮，修改字段
		{
			//SSD ssd = await hFService.SelectSSDById(hf.SalaryStandardId);
			//hf.SalaryStandardName = ssd.StandardName;//薪酬标准	
			SS ss = await hFService.SelectSSById(hf.SalaryStandardId);
			hf.SalaryStandardName = ss.StandardName;//薪酬标准
			hf.LastlyChangeTime = DateTime.Now;//档案最近一次修改时间
			HF hf2 = await hFService.SelectHFById(hf.HufId);
			hf.FileChangAmount = (short?)(hf2.FileChangAmount+1);
			hf.CheckStatus = 0;//复核状态要改变
			int row = await hFService.Update(hf);
			if(row > 0)
			{
				return RedirectToAction("CXUpd");
			}
			else
			{
				return RedirectToAction("Upd", new { id=hf.HufId});
			}
		}
		public IActionResult Delete()//删除的页面__实际是修改状态
		{
			return View();
		}
		public async Task<IActionResult> BJDel(short id)
		{
			
			int row = await hFService.BJDelete(id);
            if (row > 0)
            {
				return RedirectToAction("Delete");
			}
			else
			{
				return RedirectToAction("Delete");
			}
        }
		public IActionResult DeleteHF()//删除恢复
		{
			return View();
		}
		public async Task<IActionResult> BJDelHF(short id)//标记删除恢复
		{
			int row = await hFService.BJDeleteHF(id);
			if (row > 0)
			{
				return RedirectToAction("DeleteHF");
			}
			else
			{
				return RedirectToAction("DeleteHF");
			}
		}
		public IActionResult CDDelete()//彻底删除界面
		{
			return View();
		}
		public async Task<IActionResult> CDDel(short id)//彻底删除
		{
			int row = await hFService.Delete(id);
			if(row > 0)
			{
				return RedirectToAction("CDDelete");
			}
			else
			{
				return RedirectToAction("CDDelete");
			}
		}

		public async Task<IActionResult> SelectMC()//调动管理的查看，包含添加
		{
			await xllb();
			return View();
		}
		public async Task<IActionResult> SelectMCByName(HF hf,DateTime startDate,DateTime endDate)//调动查询里面的查询按钮
		{
			if (startDate != DateTime.MinValue)
			{
				HttpContext.Session.SetString("startDate", startDate.ToString());
			}
			else
			{
				HttpContext.Session.SetString("startDate", "");
			}
			if (endDate != DateTime.MinValue)
			{
				HttpContext.Session.SetString("endDate", endDate.ToString());
			}
			else
			{
				HttpContext.Session.SetString("endDate", "");
			}
			if (hf.FirstKindId == null)
			{
				HttpContext.Session.SetString("FirstKindName", "");
			}
			else
			{
				CFFK cffk = await cFTKService.CFFKByIdAsync(hf.FirstKindId);
				hf.FirstKindName = cffk.FirstKindName;//一级
				HttpContext.Session.SetString("FirstKindName", hf.FirstKindName);
			}

			if (hf.SecondKindId == null)
			{
				HttpContext.Session.SetString("SecondKindName", "");
			}
			else
			{
				CFSK cfsk = await cFTKService.CFSKByIdAsync(hf.SecondKindId);
				hf.SecondKindName = cfsk.SecondKindName;//二级
				HttpContext.Session.SetString("SecondKindName", hf.SecondKindName);
			}

			if (hf.ThirdKindId != null)
			{
				CFTK cftk = await hFService.cftkById(hf.ThirdKindId);
				hf.ThirdKindName = cftk.ThirdKindName;//三级
				HttpContext.Session.SetString("ThirdKindName", hf.ThirdKindName);
			}
			else
			{
				HttpContext.Session.SetString("ThirdKindName", "");
			}
			return View();
		}
	}
}
